package com.yyfax.pay.order.data.service.impl;

import javax.annotation.Resource;

import com.yyfax.pay.vo.depository.ArtificialRegisterReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.common.Log;
import com.yyfax.framework.service.SequenceService;
import com.yyfax.framework.yyfax.CryptUtil;
import com.yyfax.framework.yyfax.enums.YesNoEnum;
import com.yyfax.pay.depository.dao.EnterpriseAuthDao;
import com.yyfax.pay.depository.dao.EnterprisePaymentDao;
import com.yyfax.pay.depository.domain.EnterpriseAuth;
import com.yyfax.pay.depository.domain.EnterprisePayment;
import com.yyfax.pay.order.data.service.EnterpriseAuthService;

/**
 * @author yejc
 * @date 2017-10-19
 * @since v1.0.0
 */
@Service
public class EnterpriseAuthServiceImpl implements EnterpriseAuthService {

	private static final String ENTERPRISE_AUTH = "PAY_ENTERPRISE";
	private static final String PREFIX = "PE";
	private static final int STEP = 5;
	private static final int LEN = 10;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private SequenceService sequenceService; // 序列号生成服务
	@Resource
	private EnterpriseAuthDao enterpriseAuthDao;
	@Resource
	private EnterprisePaymentDao enterprisePaymentDao;

	@Override
	public int save(String merchantCode, String enterpriseUserId, String enterpriseName, String paymentCode, String bankCode, String cardNo, String phone) throws Exception {
		final String op = "EnterpriseAuthServiceImpl.save";
		String cardNoCip = CryptUtil.encrypt(cardNo);
		String now = DateUtil.getCurrentDateTime();
		EnterpriseAuth po = enterpriseAuthDao.selectByMerchantCodeAndEnterpriseUserId(merchantCode, enterpriseUserId);
		if (po != null) {
			logger.error(Log.op(op).msg("企业实名认证信息已存在").kv("merchantCode", merchantCode).kv("enterpriseUserId", enterpriseUserId).toString());
			return -1;
		}
		EnterpriseAuth enterpriseAuth = new EnterpriseAuth();
		enterpriseAuth.setAuthId(sequenceService.sequenceStr(ENTERPRISE_AUTH, PREFIX, STEP, LEN));
		enterpriseAuth.setMerchantCode(merchantCode);
		enterpriseAuth.setEnterpriseUserId(enterpriseUserId);
		enterpriseAuth.setEnterpriseName(enterpriseName);
		enterpriseAuth.setCardNoCip(cardNoCip);
		enterpriseAuth.setState(YesNoEnum.YES.getValue());
		enterpriseAuth.setCreateTime(now);
		enterpriseAuthDao.insert(enterpriseAuth);

		// 判断企业支付认证信息
		EnterprisePayment paymentPO = enterprisePaymentDao.selectByMerchantAndUserIdAndPaymentCode(merchantCode, enterpriseUserId, paymentCode);
		if (paymentPO != null && YesNoEnum.YES.isValue(paymentPO.getState())) {// 支付通道信息
			logger.debug(Log.op(op).msg("企业存在有效的支付认证信息，不做操作").kv("merchantCode", merchantCode).kv("enterpriseUserId", enterpriseUserId).kv("paymentCode", paymentCode).toString());
			return -1;
		}

		EnterprisePayment enterprisePayment = new EnterprisePayment();
		enterprisePayment.setAuthId(enterpriseAuth.getAuthId());
		enterprisePayment.setMerchantCode(merchantCode);
		enterprisePayment.setEnterpriseUserId(enterpriseUserId);
		enterprisePayment.setPaymentCode(paymentCode);
		enterprisePayment.setBankCode(bankCode);
		enterprisePayment.setCardNoCip(cardNoCip);
		enterprisePayment.setPhone(phone);
		enterprisePayment.setState(YesNoEnum.YES.getValue());
		enterprisePayment.setCreateTime(now);
		if (paymentPO == null) {
			enterprisePaymentDao.insert(enterprisePayment);
		} else {
			enterprisePaymentDao.update(enterprisePayment);
		}

		return 1;
	}

	@Override
	public EnterpriseAuth queryByUserIdAndMerchantCode(String merchantUserId, String merchantCode) {
		return enterpriseAuthDao.selectByMerchantCodeAndEnterpriseUserId(merchantCode, merchantUserId);
	}

	@Override
	public Boolean checkEnterpriseOpenSuccess(String merchantCode, ArtificialRegisterReq req) {
		try {
			String cardNoCip = CryptUtil.encrypt(req.getBankCardNo());
			EnterpriseAuth auth = enterpriseAuthDao.selectEnterpriseByFourElements(merchantCode, req.getUserId(), req.getArtificialName(), cardNoCip);
			if (auth != null) {
				return true;
			}
		} catch (Throwable throwable) {
			logger.warn(Log.op("EnterpriseAuthServiceImpl.queryByUserIdAndMerchantCode").toString(), throwable);
		}
		return false;
	}
}
